let open_log_flag=true;
let stack_index=2;
export class LogTool{

    public static log2(node:any,...args:any){
        var backLog=console.log||cc.log;
        if(open_log_flag){
            backLog.call(this,"%s%s:"+cc.js.formatStr.apply(backLog,args),
                (cc.js.getClassName(node)+'.js->:'),LogTool.getDateString());
        }
    }

    public static log(...args:any){
        var backLog=console.log||cc.log;
        if(open_log_flag){
            backLog.call(this,"%s%s:"+cc.js.formatStr.apply(backLog,arguments),
                LogTool.stack(stack_index),LogTool.getDateString());
        }
    }

    public static info(...args:any)
    {
        var backLog = console.log || cc.log;
        if(open_log_flag){
            backLog.call(this,"%c%s%s:"+cc.js.formatStr.apply(backLog,arguments),"color:#00CD00;",
                LogTool.stack(stack_index),LogTool.getDateString());
        }
    }

    public static warn(...args:any)
    {
        var backLog = console.log || cc.log;
        if(open_log_flag){
            backLog.call(this,"%c%s%s:"+cc.js.formatStr.apply(backLog,arguments),"color:#ee7700;",
                LogTool.stack(stack_index),LogTool.getDateString());
        }
    }

    public static err(...args:any)
    {
        var backLog = console.log || cc.log;
        if(open_log_flag){
            backLog.call(this,"%c%s%s:"+cc.js.formatStr.apply(backLog,arguments),"color:red",
                LogTool.stack(stack_index),LogTool.getDateString());
        }
    }
    private static getDateString():string{
        let d=new Date(),str=d.getHours().toString(),timeStr='';
        timeStr+=(str.length==1?"0"+str:str)+":";
        str=d.getMinutes().toString();
        timeStr+=(str.length==1?"0"+str:str)+":";
        str=d.getSeconds().toString();
        timeStr+=str;
        timeStr="["+timeStr+"]";
        return timeStr;
    }

    private static stack(index:number):string{
        var e=new Error();
        var lines=e.stack.split("\n");
        lines.shift();
        var result=[];
        lines.forEach((line)=>{
            line=line.substring(7);
            var lineBreak=line.split(" ");
            if(lineBreak.length<2){
                result.push(lineBreak[0]);
            }
            else{
                result.push({[lineBreak[0]]:lineBreak[1]});
            }
        });

        var list=[];
        if(index<result.length-1){
            for(var a in result[index]){
                list.push(a);
            }
        }

        var splitList=list[0].split(".");
        if(!splitList[1])
            return this.stack(index+=2);
        return (splitList[0]+'.js->'+splitList[1]+":");
    }
}
